﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.BaseContent;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using Job.Logic.Common;
using Job.Logic.Personal;
using Job.Logic.Company;

namespace Job.Web
{
    public partial class Index : BasePage
    {
        BaseContentLogic contentLogic = new BaseContentLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.msgTable.Visible = false;
            if (!this.IsPostBack)
            {
                //LoadAbout();
                BindNewsList();
                BindHunterJob();

                //string ctrlPath = "~/userControls/Public/BottomNews.ascx";
                //this.LoadCtrl(ctrlPath, "phBottomNews");

                BindHRList();
                BindManagerList();
                BindIndustryList();
            }
        }

        //private void LoadAbout()
        //{
        //    JobBaseContent content = contentLogic.QuickGetContentBySubjectId((int)Constants.SubjectType.CompanyDescription);
        //    if (null != content)
        //    {
        //        this.lbAboutCompany.Text = Functions.HtmlEncode( content.Description );
        //    }
        //    content = contentLogic.QuickGetContentByContentId((int)Constants.SubjectType.HunterDescription);
        //    if (null != content)
        //    {
        //        this.lbAbountHunter.Text = Functions.HtmlEncode(content.Description);
        //    }
        //    content = contentLogic.QuickGetContentByContentId((int)Constants.SubjectType.ServiceDescription);
        //    if (null != content)
        //    {
        //        this.lbAboutService.Text = Functions.HtmlEncode(content.Description);
        //    }
        //}

        ////绑定左边的新闻列表
        private void BindNewsList()
        {
            const int RowCount = 7;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyFastList, RowCount);

            if (ds != null)
            {
                this.newsList.DataSource = ds.Tables[0];
                this.newsList.DataBind();

                if (ds.Tables[0].Rows.Count >= RowCount)
                {
                    this.moreNews.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyFastList).ToString();
                    this.moreNews.Visible = true;
                }
                else
                {
                    this.moreNews.Visible = false;
                }

            }
            else
            {
                this.moreNews.Visible = false;
            }
        }


        //绑定猎头职位列表
        private void BindHunterJob()
        {
            HunterJobLogic jobLogic = new HunterJobLogic();
            DataTable dt = jobLogic.GetTopJob(20);
            if (null != dt)
            {
                jobList.DataSource = dt;
                jobList.DataBind();
            }
        }


        protected void newsList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink hlDetail = (HyperLink)e.Item.FindControl("newsLink");
                        Label lbContentId = (Label)e.Item.FindControl("newsId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");
                        BaseInfo subjectInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        hlDetail.NavigateUrl = "Item.aspx?categoryid="
                            + Convert.ToString(subjectInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (hlDetail.Text.Length > 13)
                            hlDetail.Text = hlDetail.Text.Substring(0, 13) + "...";

                        break;
                    }
            }
        }



        protected void jobList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink jobDetail = (HyperLink)e.Item.FindControl("jobLink");
                        Label lbJobNo = (Label)e.Item.FindControl("lbNo");

                        BaseInfo info = WebCommon.GetSubjectInfo((int)Constants.SubjectType.HunterPositionList);
                        jobDetail.NavigateUrl = "Item.aspx?categoryid="
                            + Convert.ToString(info.CatagoryId) + "&subjectid="
                            + Convert.ToString((int)Constants.SubjectType.HunterPositionList)
                            + "&jobno=" + lbJobNo.Text.Trim();

                        Label lbSalay = (Label)e.Item.FindControl("lbSalary");
                        if (null != lbSalay) lbSalay.Text = MemoryDictionary.GetCommonDictName(lbSalay.Text);
                        Label lbAddr = (Label)e.Item.FindControl("lbWorkAddr");
                        if (null != lbAddr) lbAddr.Text = MemoryDictionary.GetAddrName(lbAddr.Text);
                        Label lbIndustry = (Label)e.Item.FindControl("lbIndustry");
                        if (null != lbIndustry) lbIndustry.Text = MemoryDictionary.GetIndustryName(lbIndustry.Text);
                        break;
                    }

            }
        }


        //得到某个Subject所对应的前N条记录
        private DataSet QueryTopList(int subject, int number)
        {
            ArrayList param = new ArrayList();

            SqlParameter pm = new SqlParameter();
            pm.ParameterName = "subject_id";
            pm.Value = subject;
            pm.SqlDbType = SqlDbType.Int;
            param.Add(pm);

            DataSet ds = contentLogic.GetBaseContentList(number, 1, 0, "order_no", Job.Web.Framework.SortDirection.Descending, param);
            return ds;
        }



        //绑定HR频道列表
        private void BindHRList()
        {
            const int RowCount = 4;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyHRList, RowCount);

            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    Int32 id = (Int32) dt.Rows[0][0];
                    String title = (String) dt.Rows[0][1];
                    String desc = (String) dt.Rows[0][2];

                    BaseInfo hrInfo = WebCommon.GetSubjectInfo((int)Constants.SubjectType.CompanyHRList);

                    hr1LinkA.Text = title;
                    hr1LinkA.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(hrInfo.CatagoryId) + "&subjectid="
                            + Convert.ToString((int)Constants.SubjectType.CompanyHRList) 
                            + "&contentid=" + Convert.ToString(id);
                    hr1LinkB.NavigateUrl = hr1LinkA.NavigateUrl;
                    hr1Text.Text = desc;
                    dt.Rows.RemoveAt(0);
                }


                this.hrList.DataSource = ds.Tables[0];
                this.hrList.DataBind();


                if (ds.Tables[0].Rows.Count >= (RowCount -1))
                {
                    this.moreHR.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyHRList).ToString();
                    this.moreHR.Visible = true;
                }
                else
                {
                    this.moreHR.Visible = false;
                }
            }
            else
            {
                this.moreHR.Visible = false;
            }
        }

        //绑定行业动态列表
        private void BindIndustryList()
        {
            const int RowCount = 4;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyIndustryList, RowCount);

            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    Int32 id = (Int32)dt.Rows[0][0];
                    String title = (String)dt.Rows[0][1];
                    String desc = (String)dt.Rows[0][2];

                    BaseInfo industryInfo = WebCommon.GetSubjectInfo((int)Constants.SubjectType.CompanyIndustryList);

                    industry1LinkA.Text = title;
                    industry1LinkA.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(industryInfo.CatagoryId) + "&subjectid="
                            + Convert.ToString((int)Constants.SubjectType.CompanyIndustryList)
                            + "&contentid=" + Convert.ToString(id);
                    industry1LinkB.NavigateUrl = industry1LinkA.NavigateUrl;
                    industry1Text.Text = desc;
                    dt.Rows.RemoveAt(0);
                }


                this.industryList.DataSource = ds.Tables[0];
                this.industryList.DataBind();

                if (ds.Tables[0].Rows.Count >= (RowCount - 1))
                {
                    this.moreIndustry.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyIndustryList).ToString();

                    this.moreIndustry.Visible = true;
                }
                else
                {
                    this.moreIndustry.Visible = false;
                }
            }
            else
            {
                this.moreIndustry.Visible = false;
            }
        }

        //绑定管理资讯列表
        private void BindManagerList()
        {
            const int RowCount = 4;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyManagerList, RowCount);

            if (ds != null)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    Int32 id = (Int32)dt.Rows[0][0];
                    String title = (String)dt.Rows[0][1];
                    String desc = (String)dt.Rows[0][2];

                    BaseInfo manageInfo = WebCommon.GetSubjectInfo((int)Constants.SubjectType.CompanyManagerList);
                    
                    manage1LinkA.Text = title;
                    manage1LinkA.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(manageInfo.CatagoryId) + "&subjectid="
                            + Convert.ToString((int)Constants.SubjectType.CompanyManagerList)
                            + "&contentid=" + Convert.ToString(id);
                    manage1LinkB.NavigateUrl = manage1LinkA.NavigateUrl;
                    manage1Text.Text = desc;
                    dt.Rows.RemoveAt(0);
                }



                this.managerList.DataSource = ds.Tables[0];
                this.managerList.DataBind();

                if (ds.Tables[0].Rows.Count >= (RowCount - 1))
                {
                    this.moreManager.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyManagerList).ToString();
                    this.moreManager.Visible = true;
                }
                else
                {
                    this.moreManager.Visible = false;
                }
            }
            else
            {
                this.moreManager.Visible = false;
            }
        }

        protected void hrList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink hrDetail = (HyperLink)e.Item.FindControl("hrLink");
                        Label lbContentId = (Label)e.Item.FindControl("hrId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

                        BaseInfo hrInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        hrDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(hrInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (hrDetail.Text.Length > 15)
                            hrDetail.Text = hrDetail.Text.Substring(0, 15) + "...";
                        break;
                    }
            }
        }

        protected void managerList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink managerDetail = (HyperLink)e.Item.FindControl("managerLink");
                        Label lbContentId = (Label)e.Item.FindControl("managerId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

                        BaseInfo managerInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        managerDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(managerInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (managerDetail.Text.Length > 15)
                            managerDetail.Text = managerDetail.Text.Substring(0, 15) + "...";
                        break;
                    }
            }
        }

        protected void industryList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink industryDetail = (HyperLink)e.Item.FindControl("industryLink");
                        Label lbContentId = (Label)e.Item.FindControl("industryId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

                        BaseInfo industryInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        industryDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(industryInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (industryDetail.Text.Length > 15)
                            industryDetail.Text = industryDetail.Text.Substring(0, 15) + "...";
                        break;
                    }
            }

        }

        protected void btUserLogin_Click(object sender, ImageClickEventArgs e)
        {
            PersonalLogic logic = new PersonalLogic();
            JobPersonAccount account = logic.Login(tbUserName.Text.Trim());
            if (null == account)
            {
                showAlert("登录帐号不存在！");
                return;
            }

            if (account.Password.Equals(tbUserPassword.Text.Trim()))
            {
                logic.UpdateLogin(account.PersonAccountId);
                SessionPersonal spersonal = new SessionPersonal()
                {
                    LoginName = tbUserName.Text.Trim(),
                    PersonAccountid = account.PersonAccountId,
                    UserName = account.UserName
                };

                MySession session = new MySession();
                session.AddPersonal2Session(spersonal);

                Response.Redirect(WebCommon.GetApplicationPath(Request) + "Person/PersonManager.aspx?categoryid=1&ctrlid=2");
            }
            else
            {
               showAlert("用户名或密码错误！");
            }

        }

        private void showAlert(String message)
        {
            this.msgTable.Visible = true;
            this.message.InnerHtml = "<strong>" + message + "</strong>";
        }

        protected void btCompangLogin_Click(object sender, ImageClickEventArgs e)
        {
            CompanyLogic logic = new CompanyLogic();
            JobCompanyInfo company = logic.GetCompanyByLogin(tbCompanyName.Text.Trim());
            if (null == company)
            {
                showAlert("登录帐号不存在！");
                return;
            }

            if (company.LoginPswd.Equals(tbCompanyPassword.Text))
            {
                SessionCompany sCompany = new SessionCompany();
                sCompany.CompanyId = company.CompanyInfoId;
                sCompany.CompanyName = company.CompanyName;

                MySession session = new MySession();
                session.AddCompany2Session(sCompany);

                 Response.Redirect(WebCommon.GetApplicationPath(Request) + "Company/CompanyManager.aspx?categoryid=2&subjectid=2");
                return;
            }
            else
            {
                showAlert("用户名或密码错误！");
            }

        }


    }
}
